<template>
  <a-modal
    :visible="visible"
    title="新增用户"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol">
      <a-form-item ref="nickname" label="昵称" name="nickname">
        <a-input v-model:value="formState.nickname" />
      </a-form-item>
      <a-form-item ref="username" label="账户" name="username">
        <a-input v-model:value="formState.username" />
      </a-form-item>
      <a-form-item ref="password" label="密码" name="password">
        <a-input v-model:value="formState.password" />
      </a-form-item>
      <a-form-item ref="email" label="邮箱" name="email">
        <a-input v-model:value="formState.email" />
      </a-form-item>
      <a-form-item ref="phone" label="电话" name="phone">
        <a-input v-model:value="formState.phone" />
      </a-form-item>
      <a-form-item label="性别" name="gender">
        <a-radio-group v-model:value="formState.gender">
          <a-radio value="0">男</a-radio>
          <a-radio value="1">女</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="级别" name="tage">
        <a-radio-group v-model:value="formState.tage">
          <a-radio value="2">省级</a-radio>
          <a-radio value="3">市级</a-radio>
          <a-radio value="4">县级</a-radio>
          <a-radio value="5">企业</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="状态" name="enable">
        <a-switch v-model:checked="formState.enable" />
      </a-form-item>
<!--      <a-form-item label="岗位" name="postId">
          <a-select v-model:value="formState.postId">
            <a-select-option :value="post.id" v-bind:key="index" v-for="(post,index) in state.posts">{{post.name}}</a-select-option>
          </a-select>
      </a-form-item>
      <a-form-item label="部门" name="deptId">
          <a-tree-select
            v-model:value="formState.deptId"
            style="width: 100%"
            :tree-data="state.depts"
            placeholder="所属部门"
            replace
            tree-default-expand-all
            :replaceFields="replaceFields"
          >
          </a-tree-select>
      </a-form-item>-->
      <a-form-item label="所在市" name="shiid" :rules="[{ required: true, message: '请选择所在市!' }]">
        <a-select
          v-model:value="formState.shiid"
          style="width: 120px"
          :options="shiData"
          label-in-value
          @change="ChangeShi"
        >
        </a-select>
      </a-form-item>
      <a-form-item label="所在县" name="xianid" :rules="[{ required: true, message: '请选择所在县!' }]">
        <a-select
          v-model:value="formState.xianid"
          style="width: 120px"
          :options="xianData"
          @change="ChangeXian"
        >
        </a-select>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { message } from 'ant-design-vue';
import global from '@/view/global/global'
import { list } from "@/api/module/post";
import { save } from "@/api/module/user";
import { tree } from "@/api/module/dept";
import { defineComponent, reactive, ref, toRaw } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();

    const state = reactive({
      posts: [],
      depts: [],
    })

    const formState = reactive({
      enable: true,
      gender:  "0",
    });

    const formRules = {
      username: [ { required: true, message: '请输入账户', trigger: 'blur'} ],
      nickname: [ { required: true, message: '请输入昵称', trigger: 'blur'} ],
      password: [ { required: true, message: '请输入密码', trigger: 'blur'} ],
      // email: [ { required: true, message: '请输入邮箱', trigger: 'blur'} ],
      phone: [ { required: true, message: '请输入电话', trigger: 'blur'} ],
      tage: [ { required: true, message: '请选择级别', trigger: 'blur'} ],
      // shiid: [ { required: true, message: '请选择所在市', trigger: 'blur'} ],
      // xianid: [ { required: true, message: '请选择所在县', trigger: 'blur'} ],
      // postId: [ { required: true, message: '请选择岗位', trigger: 'blur'} ],
      // deptId: [ { required: true, message: '请选择部门', trigger: 'blur'} ],
      // tenantId: [ { required: true, message: '请选择租户', trigger: 'blur'} ]
    };

    const loadPost = () => {
      list({}).then((response)=>{
        state.posts = response.data;
      })
    }

    const loadTree = () => {
      tree({}).then((response)=>{
        state.depts = response.data;
      })
    }

    const submit = (e) => {
        formRef.value.validate().then(() => {
          formState.shiname=formState.shiid.label;
          formState.shiid=formState.shiid.value;
          formState.xianname=formState.xianid.label;
          formState.xianid=formState.xianid.value;
          save(toRaw(formState)).then((response)=>{
            if(response.success){
                message.success({ content: '保存成功', duration: 1 }).then(()=>{
                  cancel();
                });
            }else{
                message.error({ content: '保存失败', duration: 1 }).then(()=>{
                  cancel();
                });
            }
          })
      }).catch(error => {
          console.log('error', error);
      });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    //市县下拉联动
    const shiData=global.shiData;
    const xianData=global.xianData;
    const shi = ref();
    const xian = ref();

    const ChangeShi = value => {
      console.log(value.label);
      global.codeList({'superid':value.value}).then((response)=>{
        xianData.value = response.data.map(item =>({value : item.value, label : item.name}));
      })
    };
    const ChangeXian = value => {
    };

    /// 加载岗位
    loadPost();
    /// 加载部门
    loadTree();

    return {
      state,
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      shiData,
      xianData,
      shi,
      xian,
      ChangeShi,
      ChangeXian,

      labelCol: { span: 6 },
      wrapperCol: { span: 18 },

      replaceFields: {children:'children', title:'name', key:'id', value: 'id' }
    };
  },
});
</script>
